#!/usr/bin/env bash

#
# Runs the client tests via Docker with the expectation that the required
# environment variables have already been exported before running this script.
#
# The required environment variables include:
#
#   - $ELASTICSEARCH_VERSION
#   - $NODE_JS_VERSION
#   - $TEST_SUITE
#

set -eo pipefail

set +x
export VAULT_TOKEN=$(vault write -field=token auth/approle/login role_id="$VAULT_ROLE_ID" secret_id="$VAULT_SECRET_ID")
export CODECOV_TOKEN=$(vault read -field=token secret/clients-ci/elasticsearch-js/codecov)
unset VAULT_ROLE_ID VAULT_SECRET_ID VAULT_TOKEN
set -x

docker build \
  --file .ci/Dockerfile \
  --tag elastic/elasticsearch-js \
  --build-arg NODE_JS_VERSION=${NODE_JS_VERSION} \
  .

NODE_NAME="es1"
repo=$(pwd)
testnodecrt="/.ci/certs/testnode.crt"
testnodekey="/.ci/certs/testnode.key"
cacrt="/.ci/certs/ca.crt"

elasticsearch_image="elasticsearch"
elasticsearch_url="https://elastic:changeme@${NODE_NAME}:9200"
if [[ $TEST_SUITE != "xpack" ]]; then
  elasticsearch_image="elasticsearch-oss"
  elasticsearch_url="http://${NODE_NAME}:9200"
fi

ELASTICSEARCH_VERSION="${elasticsearch_image}:${ELASTICSEARCH_VERSION}" \
  NODE_NAME="${NODE_NAME}" \
  NETWORK_NAME="esnet" \
  DETACH=true \
  SSL_CERT="${repo}${testnodecrt}" \
  SSL_KEY="${repo}${testnodekey}" \
  SSL_CA="${repo}${cacrt}" \
  bash .ci/run-elasticsearch.sh

docker run \
  --network=esnet \
  --env "TEST_ES_SERVER=${elasticsearch_url}" \
  --env "CODECOV_TOKEN" \
  --volume $repo:/usr/src/app \
  --volume /usr/src/app/node_modules \
  --name elasticsearch-js \
  --rm \
  elastic/elasticsearch-js \
  npm run ci
